package com.jindu.producer.finance.controller;

import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.jpa.RecommendJpa;
import com.jindu.producer.finance.model.FRecommendEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "营销海报管理", tags = "RecommendController", basePath = "/recommend")
@RestController
@RequestMapping(value = "/recommend",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RecommendController {

    @Autowired
    RecommendJpa recommendJpa;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据推荐内容ID，查询推荐内容的信息", notes = "返回单个推荐内容的信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<FRecommendEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(recommendJpa.findById(id));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "【app】根据类型的id，查询推荐内容的列表信息", notes = "返回推荐内容的列表信息。")
    @RequestMapping(value = "getByType",method = RequestMethod.GET)
    public JsonResult<List<FRecommendEntity>> getByType(@RequestParam Integer type){

        if(type!=null) {
            List<FRecommendEntity> entities = recommendJpa.findByType(type);

            return JsonResult.getSuccessResult(entities);
        }else {
            return JsonResult.getFailureResult();
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "【后台】创建和编辑游戏推荐内容的相关信息，单表保存", notes = "保存推荐内容的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FRecommendEntity> save(@RequestBody FRecommendEntity recommendEntity){

        FRecommendEntity fRecommendEntity=recommendJpa.findById(recommendEntity.getId());
        int count=recommendJpa.findAll().size();

        if(fRecommendEntity==null){
            recommendEntity.setId(IdGenerator.uuid());
            recommendEntity.setSortId(count+1);
            recommendEntity.setStatus(1);
            recommendEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            recommendEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(recommendJpa.saveAndFlush(recommendEntity));
        }else {
            recommendEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(recommendEntity,fRecommendEntity, AlphaBeanUtils.getNullPropertyNames(recommendEntity));
            return JsonResult.getSuccessResult(recommendJpa.saveAndFlush(fRecommendEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除推荐内容的信息", notes = "根据推荐内容的ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(recommendJpa.deleteById(id));
    }



}


